package com.king.run.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.king.run.R;
import com.king.run.util.CheckNetwork;
import com.king.run.util.StringUtil;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

/**
 * Email 674618016@qq.com
 * Created by shuiz_wade on 2017/8/16.
 */

public class BaseFragment extends Fragment {
    private ProgressDialog progressDialog;
    private String type;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    public void httpGet(RequestParams params, final String type) {
        this.type = type;
        if (CheckNetwork.isNetworkAvailable(getActivity())) {
            showDia("正在加载中...");
            x.http().get(params, callback);
        } else {
            senToa("网络异常");
        }
    }

    public void httpGetJSON(final String type, RequestParams params, Map map) {
        this.type = type;
        JSONObject json = new JSONObject(map);
        params.setAsJsonContent(true);
        params.setBodyContent(json.toString());
        httpGet(params, type);
    }

    public void httpPostJSON(final String type, RequestParams params, Map map) {
        JSONObject json = new JSONObject(map);
        params.setAsJsonContent(true);
        params.setBodyContent(json.toString());
        http(params, type);
    }

    public void httpPutJSON(final String type, RequestParams params, Map map) {
        this.type = type;
        if (CheckNetwork.isNetworkAvailable(getActivity())) {
            showDia("正在加载中...");
            JSONObject json = new JSONObject(map);
            params.setAsJsonContent(true);
            params.setBodyContent(json.toString());
            x.http().request(HttpMethod.PUT, params, callback);
        } else {
            senToa("网络异常");
        }
    }

    public void httpPut(final String type, RequestParams params) {
        this.type = type;
        if (CheckNetwork.isNetworkAvailable(getActivity())) {
            showDia("正在加载中...");
            x.http().request(HttpMethod.PUT, params, callback);
        } else {
            senToa("网络异常");
        }
    }

    public void httpDelete(final String type, RequestParams params) {
        this.type = type;
        if (CheckNetwork.isNetworkAvailable(getActivity())) {
            showDia("正在加载中...");
            x.http().request(HttpMethod.DELETE, params, callback);
        } else {
            senToa("网络异常");
        }
    }

    public void httpPost(final String type, RequestParams params) {
        http(params, type);
    }

    private void http(RequestParams params, final String type) {
        this.type = type;
        if (CheckNetwork.isNetworkAvailable(getActivity())) {
            showDia("正在加载中...");
            x.http().post(params, callback);
        } else {
            senToa("网络异常");
        }
    }

    Callback.CommonCallback<String>
            callback = new Callback.CacheCallback<String>() {
        @Override
        public boolean onCache(String result) {
            return false;
        }

        @Override
        public void onSuccess(String result) {
            System.out.println("result" + result);
            if (type.equals("verify"))
                success(result, type);
            else {
//                BaseResult baseResult = JSON.parseObject(result, BaseResult.class);
//                if (baseResult.getCode() == 200)
//                    success(result, type);
//                else
//                    senToa(baseResult.getMessage());
            }
            hideDia();
        }

        @Override
        public void onError(Throwable ex, boolean isOnCallback) {
            error(ex.getMessage(), type);
            senToa("error" + ex.getMessage());
            hideDia();
        }

        @Override
        public void onCancelled(CancelledException cex) {

        }

        @Override
        public void onFinished() {

        }
    };

    public void success(String result, String type) {

    }

    public void error(String result, String type) {

    }

    public void senToa(String text) {
        Toast mToast = null;
        if (mToast == null) {
            mToast.makeText(getActivity(), text, Toast.LENGTH_LONG).show();
        }
    }

    public void senToa(@StringRes int id) {
        Toast mToast = null;
        if (mToast == null) {
            mToast.makeText(getActivity(), id, Toast.LENGTH_LONG).show();
        }
    }

    public void showDia(String str) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setProgressStyle(R.style.CustomProgressDialog);
        }
        progressDialog.setMessage(str);
        progressDialog.show();
    }

    public void hideDia() {
        try {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jumpBundleActvity(Class cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    public void jumpActvity(Class cls) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    public void jumpActvity(Class cls, String action) {
        Intent intent = new Intent();
        if (StringUtil.isNotBlank(action))
            intent.setAction(action);
        intent.setClass(getActivity(), cls);
        startActivity(intent);
    }

    public void jumpBundleActvityforResult(Class cls, Bundle bundle, int code) {
        Intent intent = new Intent();
        if (null != bundle)
            intent.putExtras(bundle);
        intent.setClass(getActivity(), cls);
        startActivityForResult(intent, code);
    }

    public void jumpBundleActvityforResult(Class cls, Bundle bundle, int code, String action) {
        Intent intent = new Intent();
        if (StringUtil.isNotBlank(action))
            intent.setAction(action);
        if (null != bundle)
            intent.putExtras(bundle);
        intent.setClass(getActivity(), cls);
        startActivityForResult(intent, code);
    }

    public void setResultAct(Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        getActivity().setResult(getActivity().RESULT_OK, intent);
    }
}
